<template>
  <div class="app-container">
    <div>
      <p class="dialogTitle title">当前地震事件: 
        <span v-if="currentEvent">{{ currentEvent.eventName }}</span>
        <span v-else>请在地震事件管理中设置当前地震事件</span>
      </p>
    </div>

    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="100px" v-show="showSearch">
      <el-form-item label="类别" prop="category">
        <!-- <el-input v-model="queryParams.category" placeholder="请输入类别"  clearable /> -->
        <el-select v-model="queryParams.category" placeholder="请选择类别" clearable>
          <el-option v-for="item in option" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房屋结构类型" prop="structureType">
        <el-select v-model="queryParams.structureType" placeholder="请选择房屋结构类型" clearable>
          <el-option v-for="item in option1" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
          </el-option>
        </el-select>
          <!-- <el-input v-model="queryParams.structureType" placeholder="请输入房屋结构类型"  clearable /> -->
      </el-form-item>
      <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
            <el-button type="primary" :disabled="!currentEvent" @click="handleAdd">新建</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" stripe>
        <el-table-column label="序号" align="center" width="55">
          <template #default="scope">
            <span>{{(queryParams.page - 1) * queryParams.rows + scope.$index + 1}}</span>
          </template>             
        </el-table-column>
        <el-table-column label="房屋结构类型" align="center" prop="structureType">
          <template #default="scope">
            <span style="color: #107ce1;cursor: pointer;" @click="handleEdit(scope.row)">{{getStructureType(scope.row.structureType)}}</span>
          </template>   
        </el-table-column>
        <el-table-column label="类别" align="center" prop="category" />
        <el-table-column label="创建单位" align="center" prop="company" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">         
			      <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"   @pagination="getList" /> 
      
      <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="addRef" label-width="120px" :model="form" :rules="rules">
        <el-row style="margin: 20px 0;">            
            <el-col>
              <el-form-item label="房屋结构类型" prop="structureType">
                <el-select v-model="form.structureType" placeholder="请选择房屋结构类型" clearable>
                  <el-option v-for="item in option1" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
                  </el-option>
                </el-select>
                <!-- <el-input v-model="form.structureType" placeholder="请输入房屋结构类型"  clearable :disabled="!isTrue" /> -->
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="结构类型详细说明" prop="commentInfo">
                <el-input v-model="form.commentInfo" type="textarea" maxlength="500" show-word-limit :autosize="{ minRows: 5, maxRows: 5 }" placeholder="请输入结构类型详细说明" clearable :disabled="!isTrue"/>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="类别" prop="category">
                <el-select v-model="form.category" placeholder="请选择类别" :disabled="(form.category=='预置'&&form.id)" clearable>
                  <el-option v-for="item in option" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
                <!-- <el-input v-model="form.category" placeholder="请输入类别" :disabled="(form.category=='预置'&&form.id)"/> -->
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="创建人单位" prop="company" required>
                <el-input v-model="form.company" placeholder="请输入创建单位"  disabled />
              </el-form-item>
            </el-col>   
            <el-col>
              <el-form-item label="创建人" prop="createUser" required>
                <el-input v-model="form.createUser" placeholder="请输入创建人"  disabled />
              </el-form-item>
            </el-col>            
            <el-col>
              <el-form-item label="创建时间" prop="createTime" required>
                <el-input v-model="form.createTime" placeholder="请输入创建时间"  disabled />
              </el-form-item>
            </el-col>

        </el-row>         
      </el-form>
      <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="save">保 存</el-button>
              <el-button @click="open = false">取 消</el-button>
          </div>
      </template>
    </el-dialog>          
  </div>
</template>

<script setup name="buildingStructure">
import useEventStore from "@/store/modules/event";
import { getAllList, saveData, getDetail, updateData ,getCreateInfo, del } from "@/api/comprehensiveInformation/buildingStructure.js"
const currentEvent = computed(() => useEventStore().currentEvent);
const { proxy } = getCurrentInstance();
import { getDict } from "@/api/system/dict";
const dataList = ref([]);
const option = ref([{label:'预置',value:'预置'}]);
const option1 = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const showSearch = ref(true);
const open = ref(false);
const title = ref();
const isTrue = ref(true);
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        structureType: undefined,
        category: undefined,
    },
    form: {
    },
    rules: {
      structureType:[{ required: true, message: "房屋结构类型不能为空", trigger: "blur" }],
      category:[{ required: true, message: "类别不能为空", trigger: "blur" }],
    }
})
const { queryParams, form, rules } = toRefs(data)

// 新增
function handleAdd(){
  title.value = '新建房屋结构类型信息';
  isTrue.value = true;
  getInfo();
}
function getInfo(){
  getCreateInfo().then(res=>{
    open.value = true;
    proxy.resetForm("addRef");
    form.value = res.data;
  })
}
// 编辑
function handleEdit(row){
  title.value = '编辑房屋结构类型信息';
  isTrue.value = isOk(row.xhstmId)
  console.log(isTrue.value,'lalall');
  detail(row.xhstmId);
}
// 查看
function detail(id){
  getDetail({id:id}).then(res=>{
    open.value = true;
    proxy.resetForm("addRef");
    form.value = res.data;
    option1.value.forEach(item => {
		if(item.dictItemCode == res.data.structureType) {
			form.value.structureType = item.dictItemName
		}
	})
  })
}
// 保存
function save(){
  proxy.$refs["addRef"].validate(valid => {
    if (valid) {
      if(title.value=='新建房屋结构类型信息'){
        saveData({...form.value,xsemId:currentEvent.value.id}).then(res=>{
          proxy.$modal.msgSuccess("新建成功");
          open.value = false;
          getList();
        })
      }else{
        updateData({...form.value}).then(res=>{
          proxy.$modal.msgSuccess("保存成功");
          open.value = false;
          getList();
        })
      }
    }
  })

}

// 判断是否可删可修改
function isOk(id){
  if(id.length>3){
    return true;
  }else{
    return false;
  }
}
/** 删除按钮操作 */
function handleDelete(row) {
  isTrue.value = isOk(row.xhstmId)
  if(isTrue.value){
    proxy.$modal
			.confirm("是否确认删除该信息，请确认是否要删除？", )
			.then(function() {
				return del(row.xhstmId);
			})
			.then(() => {
				getList();
				proxy.$modal.msgSuccess("删除成功");
			})
			.catch(() => {});
  }else{
    proxy.$modal.msgWarning("该数据不可删除");
  }
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 查询数据列表 */
function getList() {
  if(currentEvent.value){
    loading.value = true;
    getAllList({...queryParams.value,xsemId:currentEvent.value.id}).then(response => {
        dataList.value = response.data.rows ? response.data.rows : [];
        total.value = response.data.total;
        loading.value = false;
    }).catch(err=>{loading.value = false});
  }else{
    dataList.value = [];
    total.value = 0;
  }
}
	/** 获取字典值 */
async function getDictData() {
	await getDict("jglx").then(response => {
	    option1.value = response.data;
	});
}
function getStructureType(val){
	var name = ''
  option1.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
  return name || val;
}
onMounted(() => {
  setTimeout(()=>{
    getList();
  },200)
})
getDictData();
</script>

<style scoped lang="scss">
.title{
  margin: 0 0 20px!important;
}
</style>

